/*
 * @Author: szx
 * @Date: 2022-01-22 13:54:25
 * @LastEditTime: 2022-01-22 14:12:18
 * @Description:
 * @FilePath: \leetcode\300-399\392\392.js
 */
/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
// var isSubsequence = function (s, t) {
//     let pt = 0;
//     const m = s.length;
//     const n = t.length;
//     if (m > n) return false;
//     for (let i = 0; i < m; i++) {
//         while (pt < n && t[pt] != s[i]) {
//             pt++;
//         }
//         if (pt == n) return false;
//         pt++;
//     }
//     return true;
// };

var isSubsequence = function (s, t) {
    const n = s.length,
        m = t.length;
    let i = 0,
        j = 0;
    while (i < n && j < m) {
        if (s[i] == t[j]) {
            i++;
        }
        j++;
    }
    return i == n;
};


// dp[i][j] 表示以下标i-1为结尾的字符串s，和以下标j-1为结尾的字符串t，相同子序列的长度为dp[i][j]。
var isSubsequence = function (s, t) {
    // s、t的长度
    const [m, n] = [s.length, t.length];
    // dp全初始化为0
    const dp = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
    for (let i = 1; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
            // 更新dp[i][j]，两种情况
            if (s[i - 1] === t[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = dp[i][j - 1];
            }
        }
    }
    // 遍历结束，判断dp右下角的数是否等于s的长度
    return dp[m][n] === m ? true : false;
};

console.log(isSubsequence('axc', 'ahbxgdc'));
